package com.myorigin.weblog.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * Knife4j 配置
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
@Profile("dev")//只在 dev 环境中开启
public class Knife4jConfig {

    @Bean("webApi")
    public Docket createRestApi(){
        log.info("正在生成博客 Web 前台接口接口文档");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                //分组名称
                .groupName("Web 前台接口")
                .select()
                //这里指定 Controller 扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.myorigin.weblog.web.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 构建api信息
     */
    private ApiInfo buildApiInfo(){
        return new ApiInfoBuilder()
                .title("Weblog 博客前台接口文档")//标题
                .description("Weblog 是一款由 Spring Boot + Vue 3.2 + Vite 4.3 开发的前后端分离博客。")//描述
//                .termsOfServiceUrl("这里是url")//API 服务条款
//                .contact(new Contact("姓名","url","邮箱")) //联系人
                .version("1.0")//版本
                .build();
    }
}
